package com.googlecode.androidkurskoblenz.sensortest;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class SensorView extends LinearLayout
{
	private static final int RESOLUTION = 1000;
	private ProgressBar progressBarCurrent;
	private TextView textViewMin, textViewMax, textViewVal;
	double min, max, val;
	private String format = "%-10.3f";
	
	public SensorView(Context context)
	{
		super(context);
		init();
	}
	
	public SensorView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		init();
	}
	
	public SensorView(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		init();
	}
	
	public void setValue(double value)
	{
		val = value;
		if(val < min)
			min = val;
		if(val > max)
			max = val;

		textViewMin.setText(String.format(format , min));
		textViewMax.setText(String.format(format , max));
		textViewVal.setText(String.format(format , val));
		
		progressBarCurrent.setProgress((int) ((val-min)/(max-min)*RESOLUTION));
	}
	
	private void init()
	{
		LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		inflater.inflate(R.layout.sensor_view, this);
		
		textViewMin = (TextView) findViewById(R.id.textViewMin);
		textViewMax = (TextView) findViewById(R.id.textViewMax);
		textViewVal = (TextView) findViewById(R.id.textViewValue);
		progressBarCurrent = (ProgressBar) findViewById(R.id.progressBarCurrent);
		
		progressBarCurrent.setMax(RESOLUTION);
	}
}
